﻿
using ControlHelper;
using ControlHelper.Tools;
using ControlHelper.WPF;
using Creator.Access;
using Creator.Core;
using Creator.Core.Base;
using Creator.Core.Component;
using DesktopCreator.Controls;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static DesktopCreator.Editor;

using System.Data.SQLite;
using Creator.Core.Log;

namespace DesktopCreator
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
            btnStart.Click += btnStart_Click;
            btnStop.Click += BtnStop_Click;
            btnSet.Click += BtnSet_Click;
        }

  

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
           

        }

        void LoadComponet()
        {
            SystemData.ProcessObjects = JsonHelper.ReadJson<List<ProcessObject>>(Directory.GetCurrentDirectory() + @"\BaseConfig.json");

            if (SystemData.ProcessObjects == null)
            {
                SystemData.ProcessObjects = new List<ProcessObject>();
            }
            //重新定向引用关系
            foreach (var baseObject in SystemData.ProcessObjects)
            {
                foreach (var behaviour in baseObject.Behaviours)
                {
                    if (behaviour == null) continue;
                    behaviour.BaseObject = SystemData.ProcessObjects.Find(obj => obj.Id == behaviour.BaseObjectId);
                    behaviour.Log += LogWindow.LogInstance.ShowMsg;
                }
            }
             
            foreach (var behaviour in SystemData.ProcessObjects.SelectMany(a => a.Behaviours).OfType<RenderComponent>().ToList())
            {
                if (behaviour == null || !behaviour.Active || !behaviour.BaseObject.Active) continue;

                var fe = GetFramework(behaviour);
                if (fe == null) continue; 
                canvasMain.Children.Add(fe); 
            }  
        }


        internal static  FrameworkElement GetFramework(RenderComponent render )
        {
            if (render == null) return null;
            FrameworkElement fe = null; 
            if (render is CanvasComponent cc)
            {
                var ucp = new UCPanelObjects( )
                {
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment = VerticalAlignment.Top, 
                    Background =  cc.BackBrush
                }; 

                ucp.SetValue(Panel.ZIndexProperty, render.ZIndex);
                fe = ucp; 
                cc.OnInit += (s, e) =>
                {
                    Application.Current.Dispatcher.Invoke(
                        new Action(() => {
                         
                           ucp.Init(e as IList); 
                         }
                        
                        ));
                   
                };
                cc.OnRun += (s, e, d) =>
                {
                    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {

                        ucp.SetData(e as IList);
                    }));
                };
            } 
            return fe;
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            if (Entrey.isRunning) return;
            LoadComponet();
            Task.Run(() =>
            { 
                Entrey.Run(); 
            });
        }
        private void BtnStop_Click(object sender, RoutedEventArgs e)
        {
            canvasMain.Children.Clear();
            Entrey.Stop();
        }

        private void BtnSet_Click(object sender, RoutedEventArgs e)
        {
            Entrey.Stop();
            this.Hide();
            Editor editor = new Editor();

            editor.ShowDialog();
            Show();

        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void Window_Close(object sender, RoutedEventArgs e)
        {
            var result = MessageBox.Show("请确认关闭","确认",   MessageBoxButton.OKCancel, MessageBoxImage.Warning);
            if (result == MessageBoxResult.OK)
            {
                Close();
                Application.Current.Shutdown();
            }
        }

        private void Window_Max(object sender, RoutedEventArgs e)
        {
            
            this.WindowState = WindowState == WindowState.Normal ?WindowState.Maximized : WindowState.Normal;
        }

        private void Window_Min(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }
    } 
}
